"""
Case Type   : spq多机并行
Case Name   : alter system set设置comm_max_stream参数,取非法值合理报错
Create At   : 2023/12/15
Owner       : songjing20
Description :
    1.查看comm_max_stream默认值，查询pg_settings系统表
    2.设置为1、6000
    3.设置为非法值
    4.清理环境
Expect      :
    1.成功，1024
    2.成功
    3.合理报错
    4.成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SpqPlugin(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.common = Common('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.param = 'comm_max_stream'
        self.values = ['1', '6000']
        self.err_values = ['0', '60001', '20000.01', 'none', '*', '$', '/']

    def test_spq(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '-----step1:查看comm_max_stream默认值，查询pg_settings系统表;expect:成功-----'
        self.log.info(text)
        sql = f"show {self.param};" \
              f"select name,setting,category,short_desc,context,vartype," \
              f"source,min_val,max_val,boot_val,reset_val from pg_settings " \
              f"where name = '{self.param}';"
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.para_value = sql_res.splitlines()[2].strip()
        self.assertEqual(self.para_value, '1024', f'执行失败:{text}')
        self.log.info(self.para_value)
        self.assertIn('Maximum number of streams. | postmaster | integer | '
                      'configuration file | 1       | 60000   | 1024',
                      self.para_value, f'执行失败:{text}')

        text = '---step2:设置为1、6000;expect:成功---'
        self.log.info(text)
        for value in self.values:
            self.log.info(f'-----设置为:{value}-----')
            res = self.pri_sh.execut_db_sql(
                f"alter system set {self.param} = {value};")
            self.log.info(res)
            self.assertIn(self.constant.ALTER_SYSTEM_SUCCESS_MSG, res,
                          f'执行失败:{text}')
            self.pri_sh.restart_db_cluster()
            res = self.common.show_param(self.param)
            self.assertEqual(res, str(value), f'执行失败:{text}')

        text = '-----step3:设置为非法值;expect:报错-----'
        self.log.info(text)
        for value in self.err_values:
            self.log.info(f'-----设置为:{value}-----')
            res = self.pri_sh.execut_db_sql(
                f"alter system set {self.param} = {value};")
            self.log.info(res)
            self.assertNotIn(self.constant.ALTER_SYSTEM_SUCCESS_MSG, res,
                             f'执行失败:{text}')
            self.assertIn('ERROR', res, f'执行失败:{text}')

    def tearDown(self):
        text = '-----step4:清理环境;expect:成功-----'
        self.log.info(text)
        res = self.pri_sh.execut_db_sql(
            f"alter system set {self.param} = {self.para_value};")
        self.log.info(res)
        self.assertIn(self.constant.ALTER_SYSTEM_SUCCESS_MSG, res,
                      f'执行失败:{text}')
        self.pri_sh.restart_db_cluster()
        res = self.common.show_param(self.param)
        self.assertEqual(res, str(self.para_value), f'执行失败:{text}')
        self.log.info(f'----{os.path.basename(__file__)} end-----')
